package com.xxl.dao;

import com.xxl.pojo.Employ;
import com.xxl.util.PageUtil;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 模拟数据类
 * @author xxl
 * @date 2023/3/8
 */
@Component
public class Data {
    public static final String[] DEPARTMENT = {"运营部","行政部","财务部","质量管理部"};
    /**
     * 伪造数据库数据
     */
    public static List<Employ> employList = new ArrayList<>();
    static {
        String path = "D:\\Program Files (x86)\\idea\\IDEAproject\\spring-boot-study\\demo03_web\\src\\main\\resources\\static\\tpfile\\name.txt";
        try (FileReader reader = new FileReader(new File(path)))
        {
            char[] chars = new char[10];
            int len = 0;
            StringBuilder builder = new StringBuilder();
            while ((len = reader.read(chars)) != -1) {
                builder.append(chars, 0, len);
            }
            String[] split = builder.toString().split("、");

            for (int i = 0; i < split.length; i++) {
                employList.add(new Employ(""+new Random().nextInt(9999),""+split[i], new Random().nextInt(2),"11111111"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 返回所有用户信息
     * @author xxl
     * @return List<Employ>
     */
    public List<Employ> getAllEmploys() {
        return PageUtil.employList();
    }
   /**
    * 添加用户
    * @author xxl
    * @param  employ
    */
    public void addEmploy(Employ employ) {
        employList.add(employ);
    }
    /**
     * 修改用户
     * @author xxl
     * @param index
     * @return Employ
     */
    public Employ modifyEmploy(int index) {
        return employList.get(index);
    }

    public void  deleteEmploy(int index) {
        employList.remove(index);
    }
}
